package edu.pku.sei.transformation.codegen;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;

import edu.pku.sei.transformation.editor.model.Transformation;
import edu.pku.sei.transformation.editor.model.execution.TransformationExecution;

public class CodegenRegister {
	public static CodegenRegister INSTANCE = new CodegenRegister();
	private List<ICodegenActionFactory> actionFactories = new ArrayList<ICodegenActionFactory>();
	
	private CodegenRegister() {
		
	}
	
	public void registActionFactory(ICodegenActionFactory actionClass) {
		actionFactories.add(actionClass);
	}
	
	public List<ICodegenActionFactory> getActionFactories() {
		return actionFactories;
	}
	
	public IMenuManager getMenu(Object selectedElement) {
		IMenuManager genaction = new MenuManager("generate code");
		if (selectedElement instanceof Transformation) {
			Transformation trans = (Transformation) selectedElement;
			for (ICodegenActionFactory factory : actionFactories) {
				List<Action> actions = factory.getCodegenActions(trans);
				for (Action action : actions) {
					genaction.add(action);
				}
			}
		} else if (selectedElement instanceof TransformationExecution) {
			TransformationExecution transExe = (TransformationExecution) selectedElement;
			for (ICodegenActionFactory factory : actionFactories) {
				List<Action> actions = factory.getCodegenActions(transExe);
				for (Action action : actions) {
					genaction.add(action);
				}
			}
		}
		return genaction;
	}
}
